package com.bocai.eelly.activitys;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.BaseBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangePsdActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_forgetpwd_phone)
    EditText etForgetpwdPhone;
    @Bind(R.id.et_forgetpwd_code)
    EditText etForgetpwdCode;
    @Bind(R.id.btn_forgetpwd_getcode)
    Button btnForgetpwdGetcode;
    @Bind(R.id.et_forgetpwd_pwd)
    EditText etForgetpwdPwd;
    @Bind(R.id.et_forgetpwd_rpwd)
    EditText etForgetpwdRpwd;
    @Bind(R.id.btn_forgetpwd_commit)
    Button btnForgetpwdCommit;
    private String what;
    CountDownTimer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_psd);
        what = getIntent().getStringExtra("what");
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
    }

    public void initData() {
//        protol.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        timer = new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
//                btnRegistGetcode.setTextColor(getResources().getColor(R.color.cl_red));
                btnForgetpwdGetcode.setText((millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                btnForgetpwdGetcode.setClickable(true);
//                btnRegistGetcode.setTextColor(getResources().getColor(R.color.cl_white));
                btnForgetpwdGetcode.setText("发送");
            }
        };
    }
    private void initEvent() {
        btnForgetpwdGetcode.setOnClickListener(this);
        btnForgetpwdCommit.setOnClickListener(this);
    }

    private void initView() {
        if (what.equals("forgetpwd")) {
            ToolbarHelper.setup(this, R.string.str_title_forget_psd, R.mipmap.ic_back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });

        } else {
            ToolbarHelper.setup(this, R.string.str_title_changepsd, R.mipmap.ic_back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_forgetpwd_getcode:
                if(Utils.isEmpty(etForgetpwdPhone.getText().toString()))
                {
                    T.showShort(this,R.string.str_phone_notnull);
                }else if(11!=etForgetpwdPhone.getText().length())
                {
                    T.showShort(this,R.string.str_phone_false);
                }else
                {
                    if (what.equals("forgetpwd"))
                    {
                        getCode();
                    }else
                    {

                    }
                }
                break;

            case R.id.btn_forgetpwd_commit:
                if(Utils.isEmpty(etForgetpwdPhone.getText().toString()))
                {
                    T.showShort(this,R.string.str_phone_notnull);
                }else if(11!=etForgetpwdPhone.getText().length())
                {
                    T.showShort(this,R.string.str_phone_false);
                }else if(Utils.isEmpty(etForgetpwdCode.getText().toString()))
                {
                    T.showShort(this,R.string.str_code_notnull);

                }else if (Utils.isEmpty(etForgetpwdPwd.getText().toString()))
                {
                    T.showShort(this,getString(R.string.str_pwd_notnull));
                }else if (Utils.isEmpty(etForgetpwdRpwd.getText().toString()))
                {
                    T.showShort(this,getString(R.string.str_pwdtrue_notnull));
                }else if (!etForgetpwdPwd.getText().toString().equals(etForgetpwdRpwd.getText().toString()))
                {
                    T.showShort(this,getString(R.string.str_psd_notequals));
                }else
                {
                    if (what.equals("forgetpwd"))
                    {
                        commitPwd();
                    } else
                    {

                    }
                }
                break;
        }
    }

    private void commitPwd() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().forgetPwd(timeline,sign,etForgetpwdPhone.getText().toString(),etForgetpwdCode.getText().toString(),Utils.base64Encode(etForgetpwdPwd.getText().toString()),
                Utils.base64Encode(etForgetpwdRpwd.getText().toString()))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(ChangePsdActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        Loading.dismiss();
                        T.showShort(ChangePsdActivity.this,baseBean.getMsg());
                        if(Utils.checkResultCode(baseBean.getReturn_code()))
                        {
                            finish();
                        }
                    }
                });
    }

    private void getCode() {
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().getForgetPwdCode(timeline,sign,etForgetpwdPhone.getText().toString())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d(e.toString());
                        T.showLong(ChangePsdActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        timer.start();
                        btnForgetpwdGetcode.setClickable(false);
                        T.showShort(ChangePsdActivity.this,baseBean.getMsg());
                    }
                });
    }
}
